package com.pingan.internal.point.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.pingan.internal.point.shopping.controller.form.GoodsOrderForm;
import com.pingan.internal.point.shopping.controller.form.GoodsOrderListForm;
import com.pingan.internal.point.shopping.controller.response.DataPageList;
import com.pingan.internal.point.shopping.controller.response.ResponseData;
import com.pingan.internal.point.shopping.service.GoodsOrderService;
import com.pingan.internal.point.shopping.vo.GoodsOrderVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.pingan.internal.point.common.util.SecurityContextUtil.getUsernameFromContext;

@Controller
@RequestMapping("goods-order")
public class GoodsOrderController {
    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsOrderController.class);

    @Resource
    private GoodsOrderService goodsOrderService;


    @Value("${point.goods.upload.path}")
    private String path;

    @GetMapping("list")
    @PreAuthorize("hasRole('USER')")
    public @ResponseBody
    ResponseData list(HttpServletRequest request){
        String username = getUsernameFromContext();
        LOGGER.info("GoodsController_list_form:{}", username);
        DataPageList<GoodsOrderVo> res =  goodsOrderService.list(username);
        LOGGER.info("GoodsOrderController_list_end");
        return ResponseData.success(res);
    }

    @GetMapping("list-all")
    public @ResponseBody
    ResponseData listAdmin(GoodsOrderListForm form){
        String username = getUsernameFromContext();
        LOGGER.info("GoodsController_listadmin_form:{}", JSON.toJSONString(form));
        DataPageList<GoodsOrderVo> res =  goodsOrderService.listPage(form);
        LOGGER.info("GoodsOrderController_listadmin_end");
        return ResponseData.success(res);
    }

    @PostMapping("generate")
    @PreAuthorize("hasRole('USER')")
    @ResponseBody
    public ResponseData generateOrder(@RequestBody GoodsOrderForm form) {
        String result =goodsOrderService.generateOrder(form);
        return ResponseData.success(result);
    }



}
